<!--
 * @Author: 曹捷
 * @Date: 2020-12-01 08:51:17
 * @LastEditors: 曹捷
 * @LastEditTime: 2020-12-01 16:00:14
 * @Description: 把dialog 打造成为通过api可以调用
-->
<template>
  <uni-popup class="obit-code" :ref="createKey">
    <view class="uni-popup-dialog">
      <view class="uni-dialog-title">
        <text class="uni-dialog-title-text" :class="['uni-popup__'+options.type]">{{options.title}}</text>
      </view>
      <div v-if="options.validate" id="v_container" class="v_code"></div>
      <view class="uni-dialog-content">
        <text class="uni-dialog-content-text" v-if="options.mode === 'base'">{{options.content}}</text>
        <input v-else class="uni-dialog-input" :class="options.validate ?'t-c':''" v-model="inputCode" type="text"
          :placeholder="options.placeholder" :focus="focus">
      </view>
      <view class="uni-dialog-button-group">
        <view class="uni-dialog-button" @click="close">
          <text class="uni-dialog-button-text">取消</text>
        </view>
        <view class="uni-dialog-button uni-border-left" @click="onOk">
          <text class="uni-dialog-button-text uni-button-color">确定</text>
        </view>
      </view>

    </view>
  </uni-popup>
</template>

<script>
import GVerify from '@/app-modules/utils/js/gVerify.js'

export default {
  data () {
    return {
      createKey: '',
      inputCode: '',
      focus: false,
      options: {
        /**
       * 对话框主题 success/warning/info/error	  默认 success
       */
        type: 'error',
        /**
         * 对话框模式 base/input
         */
        mode: 'base',
        validate: false,
        title: '提示',
        content: '',
        placeholder: '请输入内容'
      }
    }
  },
  methods: {
    open (option) {
      Object.assign(this.options, option)
      this.$refs[this.createKey].open()
      if (this.options.validate) {
        setTimeout(() => {
          this.verifyCode = new GVerify('v_container')
          this.focus = true
        }, 500);
      }

    },
    close () {
      this.inputCode = ''
      this.$refs[this.createKey].close()
    },
    dialogClose (done) {
      if (this.options.close) {
        return this.options.close(this.close)
      } else {
        this.close()
      }
    },
    onOk (done) {
      if (this.options.validate) {
        let verif = this.verifyCode.validate(this.inputCode)
        if (!verif) {
          this.$show('验证码不正确')
          return
        }
      }
      if (this.options.confirm) {
        this.options.confirm(this.close)
      } else {
        this.close
      }
    }
  },
  mounted () {
  },
  created () {
    this.createKey = Math.random() * 1000;
  },
}
</script>

<style lang="scss">
.obit-code {
  background: #efefef 90%;
  width: 600rpx;
  min-height: 400rpx;
  border-radius: 16rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: #666;
  align-items: center;
  .v_code {
    width: 400rpx;
    height: 120rpx;
    margin: 0 auto;
    padding-bottom: 40rpx;
    border-radius: 10rpx;
  }
  .uni-popup-dialog {
    width: 300px;
    border-radius: 15px;
    background-color: #fff;
  }

  .uni-dialog-title {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    justify-content: center;
    padding-top: 15px;
    padding-bottom: 5px;
  }

  .uni-dialog-title-text {
    font-size: 16px;
    font-weight: 500;
  }

  .uni-dialog-content {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    justify-content: center;
    align-items: center;
    padding: 5px 15px 15px 15px;
  }

  .uni-dialog-content-text {
    font-size: 14px;
    color: #6e6e6e;
  }

  .uni-dialog-button-group {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    border-top-color: #f5f5f5;
    border-top-style: solid;
    border-top-width: 1px;
  }

  .uni-dialog-button {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */

    flex: 1;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    height: 45px;
  }

  .uni-border-left {
    border-left-color: #f0f0f0;
    border-left-style: solid;
    border-left-width: 1px;
  }

  .uni-dialog-button-text {
    font-size: 14px;
  }

  .uni-button-color {
    color: $uni-color-primary;
  }

  .uni-dialog-input {
    flex: 1;
    font-size: 14px;
  }

  .uni-popup__success {
    color: $uni-color-success;
  }

  .uni-popup__warn {
    color: $uni-color-warning;
  }

  .uni-popup__error {
    color: $uni-color-error;
  }

  .uni-popup__info {
    color: #909399;
  }
}
</style>